<html>
	<!--
		@author Jean-Lou Dupont 
	 -->
	<head>
		<style>
		/**
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 */
.code .de1, .code .de2 {font-family: 'Courier New', Courier, monospace; font-weight: normal;}
.code  {background: #EEEEEE; border: padding: 0.2em}
.code .imp {font-weight: bold; color: red;}
.code li, .code li.li1 {font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;}
.code li.li2 {font-weight: bold;}
.code .kw1 {color: #b1b100;}
.code .kw2 {color: #000000; font-weight: bold;}
.code .kw3 {color: #000066;}
.code .co1 {color: #808080; font-style: italic;}
.code .co2 {color: #808080; font-style: italic;}
.code .coMULTI {color: #808080; font-style: italic;}
.code .es0 {color: #000099; font-weight: bold;}
.code .br0 {color: #66cc66;}
.code .sy0 {color: #66cc66;}
.code .st0 {color: #ff0000;}
.code .nu0 {color: #cc66cc;}
.code .me1 {color: #006600;}
.code .me2 {color: #006600;}
.code .re0 {color: #0000ff;}
.code .re1 {color: #ff0000}

		</style>
	</head>
	
	<body>
	
		<div id="content">
		<pre class="code" style="background: #EEEEEE; border: padding: 0.2em"><ol><li class="li1"><div class="de1"><span class="kw2">&lt;?php</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* aop_object_pool</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* PHP-AOP framework</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* @author Jean-Lou Dupont</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* @package AOP</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* @category AOP</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* @pattern borg</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;* @pattern ObjectPool</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* @example </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*   </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*  # $obj is a recyclable object of class 'Test'</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;*  # put it in the recycle bin</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*  aop_object_pool::recycle( $obj );</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*  </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*  $obj2 = aop_object_pool( 'Test' );</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*  # got one from the recycling bin?</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;*  if ( is_null( $obj2 ) )</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*    $obj2 = new Test;</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* http://php-design-patterns-examples@googlecode.com/</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;*  </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*/</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="kw2">class</span> aop_object_pool <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Object Pool</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<a href="http://www.php.net/static"><span class="kw3">static</span></a> <span class="re1">$objList</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Constructor</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Returns an object from $class from the pool</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * @param $class string class name</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">	 * @return mixed</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <a href="http://www.php.net/static"><span class="kw3">static</span></a> <span class="kw2">function</span> get<span class="br0">&#40;</span> <span class="re1">$class</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="co1">// is there one?</span></div></li><li class="li2"><div class="de2">		<span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span> self<span class="sy0">::</span><span class="re1">$objList</span><span class="br0">&#91;</span> <span class="re1">$class</span> <span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">			<span class="kw1">return</span> <span class="kw2">null</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		aop_logger<span class="sy0">::</span><a href="http://www.php.net/log"><span class="kw3">log</span></a><span class="br0">&#40;</span> <span class="kw2">__METHOD__</span><span class="sy0">.</span><span class="st0">&quot; RETRIEVING an object from class($class) from the recycle bin&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2">		<span class="co1">// retrieve from list</span></div></li><li class="li1"><div class="de1">		<span class="re1">$obj</span> <span class="sy0">=</span> <a href="http://www.php.net/array_shift"><span class="kw3">array_shift</span></a><span class="br0">&#40;</span> self<span class="sy0">::</span><span class="re1">$objList</span><span class="br0">&#91;</span><span class="re1">$class</span><span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="kw1">return</span> <span class="re1">$obj</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Places an object in the recycle pool</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * @param $obj object</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <a href="http://www.php.net/static"><span class="kw3">static</span></a> <span class="kw2">function</span> recycle<span class="br0">&#40;</span> <span class="sy0">&amp;</span><span class="re1">$obj</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="re1">$class</span> <span class="sy0">=</span> <a href="http://www.php.net/get_class"><span class="kw3">get_class</span></a><span class="br0">&#40;</span> <span class="re1">$obj</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2">		<span class="co1">// is this recyclable at all?</span></div></li><li class="li1"><div class="de1">		<span class="co1">// Use 'duck typing' to inquire about the object's recycling attribute:</span></div></li><li class="li1"><div class="de1">		<span class="co1">// this additional check makes it more user friendly i.e. any object</span></div></li><li class="li1"><div class="de1">		<span class="co1">// can be sent to the recycle bin without causing problems. </span></div></li><li class="li1"><div class="de1">		<span class="re1">$recyclable</span> <span class="sy0">=</span> <span class="kw2">false</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2">		<span class="re1">$callback</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span> <span class="re1">$obj</span><span class="sy0">,</span> <span class="st0">'isRecyclable'</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">		<span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/is_callable"><span class="kw3">is_callable</span></a><span class="br0">&#40;</span> <span class="re1">$callback</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">			<span class="re1">$recyclable</span> <span class="sy0">=</span> <span class="re1">$obj</span><span class="sy0">-&gt;</span><span class="me1">isRecyclable</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span><span class="re1">$recyclable</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li2"><div class="de2">			aop_logger<span class="sy0">::</span><a href="http://www.php.net/log"><span class="kw3">log</span></a><span class="br0">&#40;</span> <span class="kw2">__METHOD__</span><span class="sy0">.</span><span class="st0">&quot; CAN'T recycle object of class($class)&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">			<a href="http://www.php.net/unset"><span class="kw3">unset</span></a><span class="br0">&#40;</span> <span class="re1">$obj</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">			<span class="kw1">return</span><span class="sy0">;</span> </div></li><li class="li1"><div class="de1">		<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2">		aop_logger<span class="sy0">::</span><a href="http://www.php.net/log"><span class="kw3">log</span></a><span class="br0">&#40;</span> <span class="kw2">__METHOD__</span><span class="sy0">.</span><span class="st0">&quot; ADDING an object from class($class) to the recycle bin&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span>		</div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		self<span class="sy0">::</span><span class="re1">$objList</span><span class="br0">&#91;</span><span class="re1">$class</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re1">$obj</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2"><span class="br0">&#125;</span><span class="co1">//end definition</span></div></li></ol></pre>
		</div>
	</body>

</html>
